package com.itheima.pinkman.controller;

import com.itheima.pinkman.pojo.ServiceInfo;
import com.itheima.pinkman.pojo.TransferServiceInfo;
import com.itheima.pinkman.service.RegistryService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.spring.annotation.MapperScan;

import java.util.List;

@RestController
@RequestMapping("/registry")
@MapperScan("com.itheima.pinkman.mapper")
public class RegistryController implements InitializingBean {

    @Autowired
    RegistryService registryService;


    /**
     * 服务注册接口
     * @param serviceInfo
     * @return
     */
    @RequestMapping("/reg")
    public String regFunction(@RequestBody ServiceInfo serviceInfo){

        registryService.add(serviceInfo);

        return "服务注册成功!";
    }

    /**
     * 服务拉取接口
     * @return
     */
    @RequestMapping("/pull")
    public List<ServiceInfo> pull(){
        List<ServiceInfo> serviceInfoList = registryService.findAll();
        return serviceInfoList;
    }

    /**
     * 服务续约,接收心跳,更新服务的接口
     * @return
     */
    @RequestMapping("/renew")
    public String renew(@RequestBody ServiceInfo serviceInfo){
        //获取servicebox中的服务
        registryService.update(serviceInfo);

        return "更新成功";
    }

    /**
     * 前端列表查询接口
     * @return
     */
    @RequestMapping("/findList")
    public List<TransferServiceInfo> findList(){
        List<TransferServiceInfo> serviceInfoList = registryService.findList();
        return serviceInfoList;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        //服务一启动检查数据库是否正常
        registryService.ifNotExistCreateDb();
    }
}
